<template>
  <view class="region-picker">
    <view class="picker-header">
      <text class="cancel" @tap="onCancel">取消</text>
      <text class="title">选择地区</text>
      <text class="confirm" @tap="onConfirm">确定</text>
    </view>
    <picker-view
      :indicator-style="indicatorStyle"
      :value="pickerValue"
      @change="onPickerChange"
      class="picker-view"
    >
      <picker-view-column>
        <view class="picker-item" v-for="(item, index) in provinces" :key="index">
          {{ item.name }}
        </view>
      </picker-view-column>
      <picker-view-column>
        <view class="picker-item" v-for="(item, index) in cities" :key="index">
          {{ item.name }}
        </view>
      </picker-view-column>
      <picker-view-column>
        <view class="picker-item" v-for="(item, index) in districts" :key="index">
          {{ item.name }}
        </view>
      </picker-view-column>
    </picker-view>
  </view>
</template>

<script>
import { regionApi } from '@/api/region';

export default {
  name: 'region-picker',
  data() {
    return {
      provinces: [],
      cities: [],
      districts: [],
      pickerValue: [0, 0, 0],
      selectedRegion: {
        province: '',
        provinceId: '',
        city: '',
        cityId: '',
        district: '',
        districtId: ''
      },
      indicatorStyle: 'height: 88rpx;'
    };
  },

  async created() {
    await this.initProvinces();
  },

  methods: {
    // 初始化省份数据
    async initProvinces() {
      try {
        const res = await regionApi.getProvinces();
        if (res.code === 200) {
          this.provinces = res.data;
          if (this.provinces.length > 0) {
            await this.loadCities(this.provinces[0].id);
          }
        }
      } catch (error) {
        console.error('初始化省份数据失败:', error);
        uni.showToast({
          title: '加载地区数据失败',
          icon: 'none'
        });
      }
    },

    // 加载城市数据
    async loadCities(provinceId) {
      try {
        const res = await regionApi.getCities(provinceId);
        if (res.code === 200) {
          this.cities = res.data;
          if (this.cities.length > 0) {
            await this.loadDistricts(this.cities[0].id);
          }
        }
      } catch (error) {
        console.error('加载城市数据失败:', error);
      }
    },

    // 加载区县数据
    async loadDistricts(cityId) {
      try {
        const res = await regionApi.getDistricts(cityId);
        if (res.code === 200) {
          this.districts = res.data;
        }
      } catch (error) {
        console.error('加载区县数据失败:', error);
      }
    },

    // 处理选择器值变化
    async onPickerChange(e) {
      const values = e.detail.value;
      
      // 如果省份改变
      if (values[0] !== this.pickerValue[0]) {
        const provinceId = this.provinces[values[0]].id;
        await this.loadCities(provinceId);
        values[1] = 0;
        values[2] = 0;
      }
      
      // 如果城市改变
      if (values[1] !== this.pickerValue[1]) {
        const cityId = this.cities[values[1]].id;
        await this.loadDistricts(cityId);
        values[2] = 0;
      }
      
      this.pickerValue = values;
      
      // 更新选中的地区信息
      this.selectedRegion = {
        province: this.provinces[values[0]]?.name || '',
        provinceId: this.provinces[values[0]]?.id || '',
        city: this.cities[values[1]]?.name || '',
        cityId: this.cities[values[1]]?.id || '',
        district: this.districts[values[2]]?.name || '',
        districtId: this.districts[values[2]]?.id || ''
      };
    },

    // 确认选择
    onConfirm() {
      this.$emit('confirm', this.selectedRegion);
    },

    // 取消选择
    onCancel() {
      this.$emit('cancel');
    }
  }
};
</script>

<style lang="scss">
.region-picker {
  background-color: #fff;
  
  .picker-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20rpx 30rpx;
    border-bottom: 1rpx solid #eee;
    
    .cancel {
      color: #666;
      font-size: 28rpx;
      padding: 10rpx 20rpx;
    }
    
    .title {
      color: #333;
      font-size: 32rpx;
      font-weight: bold;
    }
    
    .confirm {
      color: #ff5500;
      font-size: 28rpx;
      padding: 10rpx 20rpx;
    }
  }
  
  .picker-view {
    width: 100%;
    height: 480rpx;
    
    .picker-item {
      line-height: 88rpx;
      text-align: center;
      font-size: 28rpx;
      color: #333;
    }
  }
}
</style> 